;
$(function() {
	// 缓存图表
	var charts = {};

	// 柱状图
	var barChart = function(elem, xData, yData, dataName, labelFormat) {
		var chart = echarts.init(elem, 'infographic');

		chart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			tooltip : {
				trigger : 'axis'
			},
			legend : {
				data : [ dataName ]
			},
			toolbox : {
				show : true,
				feature : {
					saveAsImage : {
						show : true
					}
				}
			},
			calculable : true,
			xAxis : [ {
				type : 'category',
				axisLabel : {
					'interval' : 0,
					rotate : 0,
					formatter : function(val) {
						var a = val.split('');
						var ret = '';
						for ( var i = 0; i < a.length; i++) {
							if (i > 0 && i % 5 == 0) {
								ret += '\n';
							}
							ret += a[i];
						}
						return ret;
					}
				},
				data : xData
			} ],
			yAxis : [ {
				type : 'value'
			} ],
			series : [ {
				name : dataName,
				type : 'bar',
				itemStyle : {
					normal : {
						label : {
							show : true,
							position : 'top',
							formatter : labelFormat
						}
					}
				},
				data : yData
			} ]
		};

		chart.setOption(option);

		chart.hideLoading();

		return chart;
	};

	// 饼图
	var pieChart = function(elem, legend, data, dataName, title, formatter) {
		var chart = echarts.init(elem, 'macarons');

		chart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			title : {
				text : title,
				x : 'center'
			},
			tooltip : {
				trigger : 'item',
				formatter : formatter
			},
			legend : {
				orient : 'vertical',
				x : 'left',
				data : legend
			},
			toolbox : {
				show : true,
				feature : {
					saveAsImage : {
						show : true
					}
				}
			},
			calculable : true,
			series : [ {
				name : dataName,
				type : 'pie',
				radius : '70%',
				center : [ '50%', '50%' ],
				data : data
			} ]
		};

		chart.setOption(option);

		chart.hideLoading();

		return chart;
	};

	// 合同资金情况
	var htMoney = function() {
		$('.report-ht-money').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					if ($.isEmptyObject(data.xData) || $.isEmptyObject(data.yData)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = barChart(_this, data.xData, data.yData, '金额（￥）', '￥{c}');

					chart.on(echarts.config.EVENT.CLICK, function() {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/ht-money-click?year=' + year + '&inMoney=true');
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	htMoney();

	// 经营资金情况
	var jyMoney = function() {
		$('.report-jy-money').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					if ($.isEmptyObject(data.xData) || $.isEmptyObject(data.yData)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = barChart(_this, data.xData, data.yData, '金额（￥）', '￥{c}');

					chart.on(echarts.config.EVENT.CLICK, function() {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/ht-money-click?year=' + year + '&inMoney=');
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	jyMoney();

	// 合同资金占比
	var zbMoney = function() {
		$('.report-zb-money').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					var dd = [];
					var legend = [];
					for ( var i = 0; i < data.xData.length; i++) {
						// 最多10个
						if (i >= 10) {
							legend[10] = '其他合计';
							if (!dd[10]) {
								dd[10] = {
									name : legend[10],
									value : 0
								};
							}

							dd[10].value = dd[10].value + data.yData[i];
						}

						// 小于10
						else {
							legend[i] = data.xData[i];
							dd[i] = {
								name : legend[i],
								value : data.yData[i]
							};
						}
					}

					if ($.isEmptyObject(dd)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = pieChart(_this, [], dd, '合同金额', '', '{b}<br>{a}（￥）：{c}<br>占比：{d}%');

					chart.on(echarts.config.EVENT.CLICK, function(param) {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/ht-money-click?year=' + year + '&inMoney=true', undefined, {
							'companyA' : param.name
						});
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	zbMoney();

	// 客户项目数量占比（TOP10）
	var customReport = function() {
		$('.report-custom-num').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					var dd = [];
					var legend = [];
					for ( var i = 0; i < data.xData.length; i++) {
						// 最多10个
						if (i >= 10) {
							legend[10] = '其他合计';
							if (!dd[10]) {
								dd[10] = {
									name : legend[10],
									value : 0
								};
							}

							dd[10].value = dd[10].value + data.yData[i];
						}

						// 小于10
						else {
							legend[i] = data.xData[i];
							dd[i] = {
								name : legend[i],
								value : data.yData[i]
							};
						}
					}

					if ($.isEmptyObject(dd)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = pieChart(_this, [], dd, '数量', '', '{b}<br>{a}：{c}<br>占比：{d}%');

					chart.on(echarts.config.EVENT.CLICK, function(param) {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/ht-click?year=' + year + '&inMoney=true&lxing=true', undefined, {
							'companyA' : param.name
						});
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	customReport();

	// 投标情况
	var tbStatus = function() {
		$('.tb-status-report').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					if ($.isEmptyObject(data.xData) || $.isEmptyObject(data.yData)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = barChart(_this, data.xData, data.yData, '数量', '');

					chart.on(echarts.config.EVENT.CLICK, function(param) {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/bid-click?year=' + year, undefined, {
							statusName : param.name
						});
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	tbStatus();

	// 合同签订情况
	var htSign = function() {
		$('.ht-sign-report').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					if ($.isEmptyObject(data.xData) || $.isEmptyObject(data.yData)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = barChart(_this, data.xData, data.yData, '数量', '');

					chart.on(echarts.config.EVENT.CLICK, function(param) {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/ht-click?year=' + year + '&inMoney=&lxing=', undefined, {
							'signType' : param.name
						});
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	htSign();

	// 合同履行情况
	var htLx = function() {
		$('.ht-lx-report').each(function(i) {
			var _this = this;
			var $this = $(this);
			var url = $this.data('url');
			var id = $this.attr('id');
			var year = $('.year', $this.parent()).val();

			// 获取id
			if (!id)
				$this.attr('id', id = (('char-' + Math.random()).replace(/0\./g, '')));

			$.ajax({
				url : url,
				dataType : 'json',
				type : 'post',
				data : {
					year : year
				},
				success : function(data, textStatus, jqXHR) {
					if ($.isEmptyObject(data)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					var chart = charts[id];

					// 释放已有图表
					if (chart && chart.dispose) {
						chart.off(echarts.config.EVENT.CLICK);
						chart.dispose();
						charts[id] = false;
					}

					if ($.isEmptyObject(data.xData) || $.isEmptyObject(data.yData)) {
						$this.html('<div class="alert alert-danger" role="alert">暂无数据！</div>');
						return;
					}

					charts[id] = chart = barChart(_this, data.xData, data.yData, '数量', '');

					chart.on(echarts.config.EVENT.CLICK, function(param) {
						var year = $('.year', $this.parent()).val();
						$.suredy.loadContent($.suredy.contextPath + '/chart-event/ht-click?year=' + year + '&inMoney=&lxing=', undefined, {
							'statusName' : param.name
						});
					});
				},
				error : function(jqXHR, textStatus, errorThrown) {
				}
			});
		});
	};
	htLx();

	$('div.front-page-container').on('change', 'select.year', function() {
		var $this = $(this);
		var $report = $('.panel-body', $this.parents('.panel'));

		// 合同资金情况
		if ($report.hasClass('report-ht-money'))
			htMoney();

		// 经营资金情况
		else if ($report.hasClass('report-jy-money'))
			jyMoney();

		// 合同资金占比
		else if ($report.hasClass('report-zb-money'))
			zbMoney();

		// 客户项目数量占比（TOP10）
		else if ($report.hasClass('report-custom-num'))
			customReport();

		// 投标情况
		else if ($report.hasClass('tb-status-report'))
			tbStatus();

		// 投标情况
		else if ($report.hasClass('ht-sign-report'))
			htSign();

		// 投标情况
		else if ($report.hasClass('ht-lx-report'))
			htLx();
	});

});